#pragma once
#include<iostream>
#include<sys/types.h>
#include<sys/socket.h>
#include<arpa/inet.h>
#include<netinet/in.h>
#include<strings.h>
#include<string>

class AddInet
{
private:
    void NetToHost()
    {
        _port=ntohs(_addr.sin_port);
        _ip=ntohl(_addr.sin_addr.s_addr);
    }
    void HostToNet()
    {
        bzero(&_addr,sizeof(_addr));
        _addr.sin_family=AF_INET;
        _addr.sin_port=htons(_port);
        _addr.sin_addr.s_addr=inet_addr(_ip.c_str());
    }
public:
    AddInet(struct sockaddr_in &addr)
        :_addr(addr)
    {
        NetToHost();
    }
    AddInet(uint16_t port,const std::string &ip="0.0.0.0")
        :_port(port),_ip(ip)
    {
        HostToNet();
    }
    struct sockaddr* Addr()
    {
        return (struct sockaddr*)&_addr;
    }
    socklen_t Len()
    {
        return sizeof(_addr);
    }
    std::string Ip()
    {
        return _ip;
    }
    uint16_t Port()
    {
        return _port;
    }

    ~AddInet(){}
private:
    struct sockaddr_in _addr;
    uint16_t _port;
    std::string _ip;
};